<?php

namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Request;
use think\Session;


class Index extends Controller

{
    /**
     * 后台登录页面
     * @return \think\response\View
     */
    public function index()
    {
        return view('admin@index/login');
    }

    /**
     * 后台登录处理
     * @return \think\response\View
     */
    public function logindo(Request $request)
    {
        $p = $request->post();
        $name = $p['name'];
        $pass = md5($p['pass']);
        $row = Db::table('user1')
            ->where(['name'=>$name,'pass'=>$pass])
            ->select();
        if(empty($row)){
           $this->error('用户名或密码错误,请重试!');
        }
        $id = $row[0]['id'];
        $result = Db::field(['u.id'=>'uid','u.name'=>'un','p.Controller'=>'qc','p.function'=>'pf'])
            ->table(['user1'=>'u','jiaose'=>'j','user_j'=>'uj','permission'=>'p','jiaose_q'=>'jq'])
            ->where('u.id=uj.uid and j.id=uj.jid and j.id=jq.jid and p.id=jq.qid  and u.id ='.$id)
            ->select();

//        halt($result);
        foreach ($result as $k=>$v){
            if (in_array($v['qc'],$v)) {
                $arr['per'][]=$v['qc'].'/'.$v['pf'];
                $arr1 = $arr['per'];
            }
        }
//        foreach ($arr as $k => $v){
//
//        }
        $qx = array_unique($arr1);
      Session::set('name',[
          'name'=>$name,
          'quanxian'=>$qx
      ]);
//      halt(Session::get('name'));
        return $this->success('登录成功!','/users/main');
    }

    /**
     * 后台登出处理
     * @return \think\response\View
     */
    public function logout()
    {   Session::clear();
        $this->redirect('admin/index/index');
    }

}
